package com.doge.service.admin.category;

import com.doge.bean.pojo.category.Category;
import com.doge.bean.pojo.category.CategoryExample;
import com.doge.bean.vo.category.*;
import com.doge.exception.Param402Exception;
import com.doge.mapper.CategoryMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @program: doge
 * @description: 商品类目的服务
 * @author: Mr.Jiang
 * @create: 2021-11-30 16:46
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
    CategoryMapper categoryMapper;
    @Override
    public CategoryListVo list() {

        Integer total=0;

        ArrayList<CategoryLevelOneForCategoryListVo> categoryLevelOneForCategoryListVos = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(false);
        //查等级1的商品
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            CategoryLevelOneForCategoryListVo categoryLevelOneForCategoryListVo = new CategoryLevelOneForCategoryListVo();
            categoryLevelOneForCategoryListVo.setId(category.getId());
            categoryLevelOneForCategoryListVo.setName(category.getName());
            categoryLevelOneForCategoryListVo.setKeywords(category.getKeywords());
            categoryLevelOneForCategoryListVo.setDesc(category.getDesc());
            categoryLevelOneForCategoryListVo.setIconUrl(category.getIconUrl());
            categoryLevelOneForCategoryListVo.setPicUrl(category.getPicUrl());
            categoryLevelOneForCategoryListVo.setLevel(category.getLevel());

            total++;
            CategoryExample categoryExample2 = new CategoryExample();
            categoryExample2.createCriteria().andLevelEqualTo("L2").andDeletedEqualTo(false).andPidEqualTo(category.getId());

            //查等级二的商品
            List<Category> categories2 = categoryMapper.selectByExample(categoryExample2);



            categoryLevelOneForCategoryListVos.add(categoryLevelOneForCategoryListVo);
            ArrayList<CategoryLevelTwoForCategoryListVo> categoryLevelTwoForCategoryListVos = new ArrayList<>();
            for (Category category2 : categories2) {
                CategoryLevelTwoForCategoryListVo categoryLevelTwoForCategoryListVo = new CategoryLevelTwoForCategoryListVo();
                categoryLevelTwoForCategoryListVo.setId(category2.getId());
                categoryLevelTwoForCategoryListVo.setName(category2.getName());
                categoryLevelTwoForCategoryListVo.setKeywords(category2.getKeywords());
                categoryLevelTwoForCategoryListVo.setDesc(category2.getDesc());
                categoryLevelTwoForCategoryListVo.setIconUrl(category2.getIconUrl());
                categoryLevelTwoForCategoryListVo.setPicUrl(category2.getPicUrl());
                categoryLevelTwoForCategoryListVo.setLevel(category2.getLevel());
                categoryLevelTwoForCategoryListVos.add(categoryLevelTwoForCategoryListVo);
            }
            categoryLevelOneForCategoryListVo.setChildren(categoryLevelTwoForCategoryListVos);

        }


        CategoryListVo categoryListVo = new CategoryListVo();
        categoryListVo.setLimit(total);
        categoryListVo.setPages(1);
        categoryListVo.setTotal(total);
        categoryListVo.setPage(1);
        categoryListVo.setList(categoryLevelOneForCategoryListVos);

        return categoryListVo;
    }

    @SneakyThrows
    @Override
    public CategoryCreateVo create(Category category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        Random r = new Random();
        int i = r.nextInt(100);
        category.setSortOrder((byte) i);
        category.setDeleted(false);
        if(category.getPid()==0&&"L2".equals(category.getLevel())){
            throw new Param402Exception("请输入一级类目");
        }


        categoryMapper.insertSelective(category);
        Category categoryReturn = categoryMapper.selectByPrimaryKey(category.getId());
        CategoryCreateVo categoryCreateVo = new CategoryCreateVo();
        categoryCreateVo.setId(categoryReturn.getId());
        categoryCreateVo.setName(categoryReturn.getName());
        categoryCreateVo.setKeywords(categoryReturn.getKeywords());
        categoryCreateVo.setDesc(categoryReturn.getDesc());
        categoryCreateVo.setPid(categoryReturn.getPid());
        categoryCreateVo.setIconUrl(categoryReturn.getIconUrl());
        categoryCreateVo.setPicUrl(categoryReturn.getPicUrl());
        categoryCreateVo.setLevel(categoryReturn.getLevel());
        categoryCreateVo.setAddTime(categoryReturn.getAddTime());
        categoryCreateVo.setUpdateTime(categoryReturn.getUpdateTime());
        return categoryCreateVo;
    }

    @Override
    public CategoryResultL1 L1() {
        String L1="L1";
        List<CategoryForL1> categoryForL1s=categoryMapper.selectLevelOne(L1);
        CategoryResultL1 categoryL1Vo = new CategoryResultL1();
        categoryL1Vo.setTotal(categoryForL1s.size());
        categoryL1Vo.setLimit(categoryForL1s.size());
        categoryL1Vo.setPages(1);
        categoryL1Vo.setPage(1);
        categoryL1Vo.setList(categoryForL1s);
        return categoryL1Vo;
    }

    @Override
    public void update(Category category) {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andIdEqualTo(category.getId());
        categoryMapper.updateByExampleSelective(category,categoryExample);
    }

    @Override
    public void delete(Integer id) {
        boolean b=true;
        categoryMapper.deleteById(id,b);
    }

}
